package cmp.viewer.panels;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import util.ui.UserPrompt;

import cmp.main.Globals;
import cmp.viewer.ViewerFrame;
import cmp.viewer.controls.CollapsiblePanel;

public class MemberQueryPanel extends JPanel {
	private static final long serialVersionUID = 3740105753786330459L;

	private static final String [] SECTIONS = { "General" };
	private static final String [] SECTIONS_DESC = 	{ "" }; // description doesn't look good on applet

	public MemberQueryPanel(ViewerFrame parentFrame) {
		theParentFrame = parentFrame;
				
		createMainPanel();
		buildPanel(false);
	}
	
	private void clearFilters() {
		txtSeqID.setText("");
	}
	
	//Called by main frame to query the database
	// the 100000 limit is also in ViewerFrame
	public String getSubQuery() {
		String retVal = "";
		String tmp = getSequenceQuery();
		retVal = combineBool(retVal, tmp, true);
		retVal = combineBool(retVal, "unitrans.aaSeq IS NOT NULL", true);
		if (tmp.equals("")) retVal += " limit 100000";
		return retVal;
	}
	
	public String getQuerySummary() {
		String retVal = "";
		retVal = combineSummary(retVal, getSequenceQuerySummary());
		
		if(retVal.length() == 0)
			return "Show All (limit 100000)";
		return retVal;
	}
	
	private String getSequenceQuery() {
		String retVal = "";
		if(txtSeqID.getText().trim().length() > 0)
			retVal = combineBool(retVal, 
					"unitrans.UTstr LIKE '%" + txtSeqID.getText().trim() + "%'", true);
		return retVal;
	}

	private String getSequenceQuerySummary() {
		String retVal = "";
		if(txtSeqID.getText().trim().length() > 0)
			retVal = combineSummary(retVal, 
					"Sequence ID contains '" + txtSeqID.getText().trim() + "'");
		return retVal;
	}

	private void createMainPanel() {
		mainPanel = new JPanel();

		createButtonPanel();
		//createShowButtonPanel();
		createSections();
	}
	
	private void createButtonPanel() {
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
		buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		buttonPanel.setBackground(theParentFrame.getSettings().getFrameSettings().getBGColor());
		
		btnSearch = new JButton("Find Sequence");
		btnSearch.setBackground(Globals.FUNCTIONCOLOR);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ListDataPanel newPanel = new ListDataPanel(theParentFrame, "Filter " + theParentFrame.getNextLabelNum("Filter ") + ": ", ListDataPanel.MODE_UNITRANS);
				theParentFrame.addResultPanel(newPanel, newPanel.getName(), newPanel.getSummary());
			}
		});
		
		btnClear = new JButton("Clear");
		btnClear.setBackground(theParentFrame.getSettings().getFrameSettings().getBGColor());
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearFilters();
			}
		});
		
		btnHelp = new JButton("Help");
		btnHelp.setBackground(Globals.HELPCOLOR);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UserPrompt.displayHTMLResourceHelp(theParentFrame, "Member Filter Help", "html/viewMultiTCW/MemberQueryPanel.html");
			}
		});
		
		buttonPanel.add(btnSearch);
		buttonPanel.add(Box.createHorizontalStrut(5));
		buttonPanel.add(btnClear);
		buttonPanel.add(Box.createHorizontalStrut(5));
		buttonPanel.add(btnHelp);
		buttonPanel.setMaximumSize(buttonPanel.getPreferredSize());
	}
	
	//Left out for now since we don't need expand/collapse all
	private void createShowButtonPanel() {
		showButtonPanel = new JPanel();
		showButtonPanel.setLayout(new BoxLayout(showButtonPanel, BoxLayout.LINE_AXIS));
		showButtonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		showButtonPanel.setBackground(theParentFrame.getSettings().getFrameSettings().getBGColor());
		
		btnExpand = new JButton("Expand All");
		btnExpand.setBackground(theParentFrame.getSettings().getFrameSettings().getBGColor());
		btnExpand.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for(int x=0; x<theSections.length; x++)
					theSections[x].expand();
			}
		});
		
		btnCollapse = new JButton("Collapse All");
		btnCollapse.setBackground(theParentFrame.getSettings().getFrameSettings().getBGColor());
		btnCollapse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for(int x=0; x<theSections.length; x++)
					theSections[x].collapse();
			}
		});

		showButtonPanel.add(btnExpand);
		showButtonPanel.add(Box.createHorizontalStrut(10));
		showButtonPanel.add(btnCollapse);
		
		showButtonPanel.setMaximumSize(showButtonPanel.getPreferredSize());
	}
	
	private void createSections() {
		theSections = new CollapsiblePanel[SECTIONS.length];
		
		for(int x=0; x<theSections.length; x++) {
			theSections[x] = new CollapsiblePanel(SECTIONS[x], SECTIONS_DESC[x]);
			theSections[x].setAlignmentX(Component.LEFT_ALIGNMENT);
		}
		theSections[0].add(createSequencePanel());
	}
	
	private JPanel createSequencePanel() {
		JPanel retVal = new JPanel();
		retVal.setAlignmentX(Component.LEFT_ALIGNMENT);
		retVal.setBackground(theParentFrame.getSettings().getFrameSettings().getBGColor());
		retVal.setLayout(new BoxLayout(retVal, BoxLayout.PAGE_AXIS));
	
		lblSeqID = new JLabel("Sequence ID substring");
		txtSeqID = new JTextField(30);
		txtSeqID.setMaximumSize(txtSeqID.getPreferredSize());
		txtSeqID.setMinimumSize(txtSeqID.getPreferredSize());
		
		pnlSequence = getPanel(true);
		pnlSequence.add(lblSeqID);
		pnlSequence.add(Box.createHorizontalStrut(10));
		pnlSequence.add(txtSeqID);
		
		retVal.add(pnlSequence);
		return retVal;
	}
	
	
	private JPanel getPanel(boolean rowLayout) {
		JPanel tempRow = new JPanel();
		tempRow.setAlignmentX(Component.LEFT_ALIGNMENT);
		tempRow.setBackground(theParentFrame.getSettings().getFrameSettings().getBGColor());
		int layout = BoxLayout.LINE_AXIS;
		if(!rowLayout)
			layout = BoxLayout.PAGE_AXIS;
		
		tempRow.setLayout(new BoxLayout(tempRow, layout));
		return tempRow;
	}
	
	private void buildPanel(boolean expandAll) {
		removeAll();
		setBackground(theParentFrame.getSettings().getFrameSettings().getBGColor());
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		mainPanel.removeAll();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
		mainPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		mainPanel.setBackground(theParentFrame.getSettings().getFrameSettings().getBGColor());
		
		add(buttonPanel);
		add(Box.createVerticalStrut(5));
		if(showButtonPanel != null) {
			add(showButtonPanel);
			add(Box.createVerticalStrut(10));
		}		

		for(int x=0; x<theSections.length; x++) {
			if(expandAll)
				theSections[x].expand();
			mainPanel.add(theSections[x]);
		}

		JScrollPane sPane = new JScrollPane(mainPanel);
		add(sPane);
}
	
	//This is repeated in every query panel.. necessary evil for now
	private static String combineBool(String left, String right, boolean isAND) {
		if(left == null || left.length() == 0)
			return right;
		if(right == null || right.length() == 0)
			return left;
		if(isAND)
			return left + " AND " + right;
		return left + " OR " + right;
	}
	
	private static String combineSummary(String left, String right) {
		if(left == null || left.length() == 0)
			return right;
		if(right == null || right.length() == 0)
			return left;
		return left + ", " + right;
	}
	
	private ViewerFrame theParentFrame = null;
	
	//Top button panel
	private JPanel buttonPanel = null;
	private JButton btnSearch = null;
	private JButton btnClear = null;
	private JButton btnHelp = null;
	
	//Main panel
	private JPanel mainPanel = null;
	private JPanel showButtonPanel = null;
	//Left out for now since we don't need expand/collapse all
	private JButton btnExpand = null, btnCollapse = null;
	private CollapsiblePanel [] theSections = null;
	
	//Query controls
	private JPanel pnlSequence = null;
	private JLabel lblSeqID = null;
	private JTextField txtSeqID = null;
}
